﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Document_Management
{
    public partial class frmxiulijilu : Form
    {
        static string connectionString = ConfigurationManager.ConnectionStrings["庫存管理"].ConnectionString;
        SqlConnection con = new SqlConnection(connectionString);
        string str = null;
        bool b量具Search, b量具Insert, b量具Edit, b量具Del;
        public frmxiulijilu(bool b量具Search, bool b量具Insert, bool b量具Edit, bool b量具Del)
        {
            InitializeComponent();
            this.b量具Search = b量具Search;
            this.b量具Insert = b量具Insert;
            this.b量具Edit = b量具Edit;
            this.b量具Del = b量具Del;
        }       

        private void btnSearch修理_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                //if (Ck領用日期.Checked == true)
                //{

                //    //領用紀錄load();
                //}
                //if (Ck歸還日期.Checked == true)
                //{

                //    //領用紀錄load();
                //}
                con.Open();
                string sp = "sp_修理_尋找";
                SqlCommand cmd = new SqlCommand(sp, con);
                cmd.CommandType = CommandType.StoredProcedure;
                if (txt量具編號.Text != "") cmd.Parameters.AddWithValue("@量具編號", txt量具編號.Text);
                if (txt量具名稱.Text.ToString() != "") cmd.Parameters.AddWithValue("@量具名稱", txt量具名稱.Text);
                //if (dateTimePicker_To_返庫日期.Text != "") cmd.Parameters.AddWithValue("@返庫日期", DateTime.Parse(dateTimePicker_To_返庫日期.Text));
                //if (dateTimePicker_From_修理日期.Text != "") cmd.Parameters.AddWithValue("@修理日期", DateTime.Parse(dateTimePicker_From_修理日期.Text));
                if (txt修理人.Text != "") cmd.Parameters.AddWithValue("@修理人代號", txt修理人.Text);
                if (txt受理人.Text.ToString() != "") cmd.Parameters.AddWithValue("@受理人代號", txt受理人.Text);
                cmd.Parameters.AddWithValue("@datefrom", dateTimePicker_To_返庫日期.Value);
                cmd.Parameters.AddWithValue("@todate", todate.Value);
                cmd.Parameters.AddWithValue("@datefrom2", dateTimePicker_From_修理日期.Value);
                cmd.Parameters.AddWithValue("@todate2", todate2.Value);
                SqlDataReader sqlreader = cmd.ExecuteReader();
                DataTable dt = new DataTable();
                //dt.Columns.Add("項次");//add new 項次 column
                dt.Columns.Add("量具編號");
                dt.Columns.Add("量具狀態");
                dt.Columns.Add("量具名稱");
                dt.Columns.Add("規格型號");
                dt.Columns.Add("存放地點");
                dt.Columns.Add("測量範圍");
                dt.Columns.Add("數量");
                dt.Columns.Add("修理日期");
                dt.Columns.Add("返庫日期");
                dt.Columns.Add("修理人代號");
                dt.Columns.Add("故障描述");
                dt.Columns.Add("受理人代號");
                dt.Columns.Add("修理內容");
                dt.Columns.Add("備註");
                //int k=1;
                while (sqlreader.Read())
                {
                    DataRow dtrows = dt.NewRow();
                    //dtrows["項次"]=k.ToString();
                    dtrows["量具編號"] = sqlreader["量具編號"];
                    dtrows["量具狀態"] = sqlreader["量具狀態"];
                    dtrows["量具名稱"] = sqlreader["量具名稱"];
                    dtrows["規格型號"] = sqlreader["規格型號"];
                    dtrows["存放地點"] = sqlreader["存放地點"];
                    dtrows["測量範圍"] = sqlreader["測量範圍"];
                    dtrows["數量"] = sqlreader["數量"];
                    dtrows["修理日期"] = sqlreader["修理日期"];
                    dtrows["返庫日期"] = sqlreader["返庫日期"];
                    dtrows["修理人代號"] = sqlreader["修理人代號"];
                    dtrows["故障描述"] = sqlreader["故障描述"];
                    dtrows["受理人代號"] = sqlreader["受理人代號"];
                    dtrows["修理內容"] = sqlreader["修理內容"];
                    dtrows["備註"] = sqlreader["備註"];
                    dt.Rows.Add(dtrows);
                    //k++;
                }
                dataGV修理.DataSource = dt;
                PublicClass.setRowNumber(dataGV修理);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            Load修理();
            ResetText修理記錄();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmxiulijilu_Load(object sender, EventArgs e)
        {
            if (!b量具Insert)
            {
                btnTra.Enabled = false;
                btnAddnewShouli.Enabled = false;
                btnAddnewXiuli.Enabled = false;
            }
            if (!b量具Search)
            {
                btnSearch修理.Enabled = false;
            }
            if (!b量具Edit)
            {
                btnEditXiuli.Enabled = false;
                btnEditShouli.Enabled = false;
            }
            if (!b量具Del)
            {
                btnDeleteShouli.Enabled = false;
                btnDeleteXiuli.Enabled = false;
            }
            Load修理();
            受理人load();
            修理人load();
            ResetText受理人();
            ResetText修理人();
            ResetText修理記錄();
        }
        
        public void Load修理()
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter("sp_修理_load", con);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataTable dt = new DataTable();
                da.Fill(dt);
                //dataGV修理.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                dataGV修理.DataSource = dt;
                dataGV修理.Columns[0].Width = 0;
                PublicClass.setRowNumber(dataGV修理);
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        public void ResetText修理記錄()
        {
            dateTimePicker_From_修理日期.ResetText();
            dateTimePicker_To_返庫日期.ResetText();
            txt修理人.ResetText();
            txt受理人.ResetText();
            txt量具名稱.ResetText();
            txt量具編號.ResetText();
        }

        public void 受理人load()
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter("sp_修理_受理人_load", con);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGV受理人.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                dataGV受理人.DataSource = dt;
                //dataGV受理人.Columns[0].Width = 0;
                PublicClass.setRowNumber(dataGV受理人);
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }
        
        public void ResetText受理人()
        {
            txt受理人代號.ResetText();
            txt受理人名稱.ResetText();
        }

        public void 修理人load()
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter("sp_修理人_load", con);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGV修理人.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                dataGV修理人.DataSource = dt;
                //dataGV受理人.Columns[0].Width = 0;
                PublicClass.setRowNumber(dataGV修理人);
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }
        public void ResetText修理人()
        {
            txt修理人編碼.ResetText();
            txt修理人名稱.ResetText();
        }

        private void dataGV修理_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            PublicClass.setRowNumber(dataGV修理);
        }

        string tbx0, tbx1, tbx2, tbx3, tbx4, tbx5, tbx6, tbx7, tbx8, tbx9;
        string ptx1, ptx2, ptx3, ptx4, ptx5;
        string xiuli;
        private void btnTra_Click(object sender, EventArgs e)
        {
            xiuli = "2";
            int r = dataGV修理.CurrentCell.RowIndex;
            string id0 = dataGV修理.Rows[r].Cells[0].Value.ToString();
            string id1 = dataGV修理.Rows[r].Cells[1].Value.ToString();
            string id6 = dataGV修理.Rows[r].Cells[2].Value.ToString();
            string id2 = dataGV修理.Rows[r].Cells[4].Value.ToString();
            string id3 = dataGV修理.Rows[r].Cells[5].Value.ToString();
            string id4 = dataGV修理.Rows[r].Cells[3].Value.ToString();
            string id5 = dataGV修理.Rows[r].Cells[6].Value.ToString();

            string id7 = dataGV修理.Rows[r].Cells[8].Value.ToString();
            string id8 = dataGV修理.Rows[r].Cells[9].Value.ToString();
            //string id9 = dataGV修理.Rows[r].Cells[10].Value.ToString();
            string id10 = dataGV修理.Rows[r].Cells[10].Value.ToString();
            string id11 = dataGV修理.Rows[r].Cells[11].Value.ToString();
            string id12 = dataGV修理.Rows[r].Cells[12].Value.ToString();
            string id13 = dataGV修理.Rows[r].Cells[13].Value.ToString();
            string id14 = dataGV修理.Rows[r].Cells[14].Value.ToString();

            tbx0 = id0;
            tbx1 = id1; //tb1 0
            tbx2 = id2; //cb1 1
            tbx3 = id3; //tb2 2
            tbx4 = id4; //tb3 3
            tbx5 = id5; //cb2 4
            tbx6 = id6;
            ptx1 = id7;
            ptx2 = id8;
            //pt3 = id9;
            ptx4 = id10;
            ptx5 = id11;
            tbx7 = id12;
            tbx8 = id13;
            tbx9 = id14;

            frmXiuLi frm = new frmXiuLi(xiuli, tbx0, tbx1, tbx2, tbx3, tbx4, tbx5, tbx6, ptx1, ptx2, ptx3, ptx4, ptx5, tbx7, tbx8, tbx9, b量具Search, b量具Insert, b量具Edit, b量具Del);
            frm.ShowDialog();
            Load修理();
        }

        /// <summary>
        /// Shouli
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="Phucnx"></param>
        private void btnDeleteShouli_Click(object sender, EventArgs e)
        {
            try
            {
                if (txt受理人代號.Text == "") { MessageBox.Show("請您輸入受理人代號刪除"); }
                else
                {

                    DialogResult answer = MessageBox.Show("您確定要出入受理人代號碼", "警報", MessageBoxButtons.YesNo);
                    if (answer == DialogResult.Yes)
                    {
                        con.Open();
                        string sp_delete = "sp_修理_受理人_刪除";
                        SqlCommand sqldelete = new SqlCommand(sp_delete, con);
                        sqldelete.CommandType = CommandType.StoredProcedure;
                        sqldelete.Parameters.AddWithValue("@受理人代號", txt受理人代號.Text);
                        MessageBox.Show("刪除成功");
                        sqldelete.ExecuteNonQuery();
                        受理人load();
                        con.Close();
                        ResetText受理人();
                        return;
                    }
                    else
                    {
                        受理人load();
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                受理人load();
                return;
            }
        }

        private void btnEditShouli_Click(object sender, EventArgs e)
        {
            try
            {
                if (txt受理人代號.Text == "") { MessageBox.Show("請您輸入受理人代號更新"); }
                else
                {
                    con.Open();
                    SqlCommand cmd2;
                    String sql = "SELECT COUNT(*) FROM 修理_受理人 WHERE 受理人代號='" + str + "'";
                    cmd2 = new SqlCommand(sql, con);
                    if ((int)cmd2.ExecuteScalar() > 0)
                    {
                        string sp_update = "sp_修理_受理人_修改";
                        cmd2 = new SqlCommand(sp_update, con);
                        cmd2.CommandType = CommandType.StoredProcedure;
                        //con.Open();
                        cmd2.Parameters.AddWithValue("@受理人代號", str);
                        cmd2.Parameters.AddWithValue("@受理人代號2", txt受理人代號.Text);
                        cmd2.Parameters.AddWithValue("@姓名", txt受理人名稱.Text);
                        cmd2.ExecuteNonQuery();
                        MessageBox.Show("更新成功");
                        受理人load();
                        ResetText受理人();
                        con.Close();
                        return;
                    }
                    else
                    {
                        MessageBox.Show("Error! '" + txt受理人代號.Text.ToString() + "' 不由來");
                        con.Close();
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                受理人load();
                return;
            }
        }

        private void btnAddnewShouli_Click(object sender, EventArgs e)
        {
            try
            {
                if (txt受理人代號.Text == "")
                {
                    MessageBox.Show("受理人代號不正確", "錯誤", MessageBoxButtons.OK);
                    txt受理人代號.Focus();
                    return;
                }
                //bat dau insert vao database

                con.Open();
                string sp_insert = "sp_修理_受理人_新增";
                SqlCommand cmd = new SqlCommand(sp_insert, con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@受理人代號", txt受理人代號.Text);
                cmd.Parameters.AddWithValue("@姓名", txt受理人名稱.Text);
                cmd.ExecuteNonQuery();
                MessageBox.Show("新增成功");
                受理人load();
                ResetText受理人();
                con.Close();
                //reset all text
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                受理人load();
                return;
            }
        }

        private void btnResetShouli_Click(object sender, EventArgs e)
        {
            ResetText受理人();
            //受理人load();
        }
        
        private void dataGV受理人_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            PublicClass.setRowNumber(dataGV受理人);
        }
        /// <summary>
        /// Xiuli
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="Phucnx"></param>
        private void btnEditXiuli_Click(object sender, EventArgs e)
        {
            try
            {
                if (txt修理人編碼.Text == "") { MessageBox.Show("請您輸入修理人代號更新"); }
                else
                {
                    con.Open();
                    SqlCommand cmd2;
                    String sql = "SELECT COUNT(*) FROM 修理人 WHERE 修理人代號='" + str + "'";
                    cmd2 = new SqlCommand(sql, con);
                    if ((int)cmd2.ExecuteScalar() > 0)
                    {
                        string sp_update = "sp_修理人_修改";
                        cmd2 = new SqlCommand(sp_update, con);
                        cmd2.CommandType = CommandType.StoredProcedure;
                        //con.Open();
                        cmd2.Parameters.AddWithValue("@修理人代號", str);
                        cmd2.Parameters.AddWithValue("@修理人代號2", txt修理人編碼.Text);
                        cmd2.Parameters.AddWithValue("@姓名", txt修理人名稱.Text);
                        cmd2.ExecuteNonQuery();
                        MessageBox.Show("更新成功");
                        修理人load();
                        ResetText修理人();
                        con.Close();
                        return;
                    }
                    else
                    {
                        MessageBox.Show("Error! '" + txt修理人編碼.Text.ToString() + "' 不由來");
                        con.Close();
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                受理人load();
                return;
            }
        }

        private void btnAddnewXiuli_Click(object sender, EventArgs e)
        {
            try
            {
                if (txt修理人編碼.Text == "")
                {
                    MessageBox.Show("修理人代號不正確", "錯誤", MessageBoxButtons.OK);
                    txt受理人代號.Focus();
                    return;
                }
                //bat dau insert vao database

                con.Open();
                string sp_insert = "sp_修理人_新增";
                SqlCommand cmd = new SqlCommand(sp_insert, con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@修理人代號", txt修理人編碼.Text);
                cmd.Parameters.AddWithValue("@姓名", txt修理人名稱.Text);
                cmd.ExecuteNonQuery();
                MessageBox.Show("新增成功");
                修理人load();
                ResetText修理人();
                con.Close();
                //reset all text
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                修理人load();
                return;
            }
        }

        private void btnDeleteXiuli_Click(object sender, EventArgs e)
        {
            try
            {
                if (txt修理人編碼.Text == "") { MessageBox.Show("請您輸入修理人代號刪除"); }
                else
                {
                    DialogResult answer = MessageBox.Show("您確定要出入修理人代號碼", "警報", MessageBoxButtons.YesNo);
                    if (answer == DialogResult.Yes)
                    {
                        con.Open();
                        string sp_delete = "sp_修理人_刪除";
                        SqlCommand sqldelete = new SqlCommand(sp_delete, con);
                        sqldelete.CommandType = CommandType.StoredProcedure;
                        sqldelete.Parameters.AddWithValue("@修理人代號", txt修理人編碼.Text);
                        MessageBox.Show("刪除成功");
                        sqldelete.ExecuteNonQuery();
                        修理人load();
                        con.Close();
                        ResetText修理人();
                        return;
                    }
                    else
                    {
                        修理人load();
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                修理人load();
                return;
            }
        }

        private void btnResetXiuli_Click(object sender, EventArgs e)
        {
            ResetText修理人();
            //修理人load();
        }

        private void dataGV修理人_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            PublicClass.setRowNumber(dataGV修理人);
        }

        private void tab修理紀錄_SelectedIndexChanged(object sender, EventArgs e)
        {
            PublicClass.setRowNumber(dataGV修理人);
            ResetText修理人();
            PublicClass.setRowNumber(dataGV受理人);
            ResetText受理人();
            PublicClass.setRowNumber(dataGV修理);
            ResetText修理記錄();
        }

        private void dataGV受理人_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int r = e.RowIndex;
                str = dataGV受理人.Rows[r].Cells[0].Value.ToString();
                txt受理人代號.Text = dataGV受理人.Rows[r].Cells[0].Value.ToString();
                txt受理人名稱.Text = dataGV受理人.Rows[r].Cells[1].Value.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }

        private void dataGV修理人_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int r = e.RowIndex;
                str = dataGV修理人.Rows[r].Cells[0].Value.ToString();
                txt修理人編碼.Text = dataGV修理人.Rows[r].Cells[0].Value.ToString();
                txt修理人名稱.Text = dataGV修理人.Rows[r].Cells[1].Value.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }

        private void txt修理人_TextChanged(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                String temp = "";
                string strcmd = String.Format("select * from 修理人 where 修理人代號=N'{0}'", txt修理人.Text);
                SqlCommand cmd = new SqlCommand(strcmd, con);
                var x = cmd.ExecuteReader();
                //int icount=0;
                while (x.Read()) temp = x[1].ToString();
                if (temp == "") lbName修理人.Text = "";
                else lbName修理人.Text = temp;
                con.Close();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void txt受理人_TextChanged(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                String temp = "";
                string strcmd = String.Format("select * from 修理_受理人 where 受理人代號=N'{0}'", txt受理人.Text);
                SqlCommand cmd = new SqlCommand(strcmd, con);
                var x = cmd.ExecuteReader();
                //int icount=0;
                while (x.Read()) temp = x[1].ToString();
                if (temp == "") lbName受理人.Text = "";
                else lbName受理人.Text = temp;
                con.Close();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void btn修理人Search_Click(object sender, EventArgs e)
        {
            frmXiuliRen frm = new frmXiuliRen(txt修理人);
            frm.ShowDialog();
        }

        private void btnSearch受理人_Click(object sender, EventArgs e)
        {
            frmShouliRen frm = new frmShouliRen(txt受理人);
            frm.ShowDialog();
        }

        private void txt量具編號_TextChanged(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                String temp = "";
                string strcmd = String.Format("select * from 量具訊息 where 量具編號=N'{0}'", txt量具編號.Text);
                SqlCommand cmd = new SqlCommand(strcmd, con);
                var x = cmd.ExecuteReader();
                //int icount=0;
                while (x.Read()) temp = x[2].ToString();
                if (temp == "") LbName量具.Text = "";
                else LbName量具.Text = temp;
                con.Close();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void btnSearch量具_Click(object sender, EventArgs e)
        {
            frmSearchLiangjubianhao frm = new frmSearchLiangjubianhao(txt量具編號);
            frm.ShowDialog();
        }

    }
}
